package com.github.wxiaoqi.security.common.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.util.Collections;
import java.util.Map;

/**
 * Created by qyf on 2016/8/18.
 * Jackson处理工具
 */
public final class JacksonUtil {

    public static ObjectMapper objectMapper=new ObjectMapper();

    /**
     * 使用泛型方法，把json字符串转换为相应的JavaBean对象。
     * (1)转换为普通JavaBean：readValue(json,Student.class)
     * (2)转换为List,如List<Student>,将第二个参数传递为Student
     * [].class.然后使用Arrays.asList();方法把得到的数组转换为特定类型的List
     *
     * @param jsonStr
     * @param valueType
     * @return
     */
    public static <T> T readValue(String jsonStr, Class<T> valueType) throws Exception{
        try {
            return objectMapper.readValue(jsonStr, valueType);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
    public static <T> T readValue(File file, Class<T> valueType) throws Exception{
        try {
            return objectMapper.readValue(file, valueType);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
    /**
     * json数组转List
     * new TypeReference<List<Class>>() {}
     * @param jsonStr
     * @param valueTypeRef
     * @return
     */
    public static <T> T readValue(String jsonStr, TypeReference<T> valueTypeRef) throws Exception{

        try {
            return objectMapper.readValue(jsonStr, valueTypeRef);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

//        return null;
    }
    public static <T> T readValue(File file, TypeReference<T> valueTypeRef) throws Exception{
        try {
            return objectMapper.readValue(file, valueTypeRef);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
    /**
     * 把JavaBean转换为json字符串
     *
     * @param object
     * @return
     */
    public static String toJSon(Object object) throws Exception{

        try {
            return objectMapper.writeValueAsString(object);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
    public static String toJSonIgnoreException(Object object){

        try {
            return objectMapper.writeValueAsString(object);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
    public static Map toMap(String jsonStr) throws Exception{

        try {
            return objectMapper.readValue(jsonStr,Map.class);
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }
    public static Map toMapIgnoreException(String jsonStr){

        try {
            return objectMapper.readValue(jsonStr,Map.class);
        }catch (Exception e){
            e.printStackTrace();
            return Collections.emptyMap();
        }
    }
    public static Map toMap(Object object) throws Exception{

        try {
            return objectMapper.readValue(toJSon(object),Map.class);
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }

    }
    public static Map toMapIgnoreException(Object object){

        try {
            return objectMapper.readValue(toJSon(object),Map.class);
        }catch (Exception e){
            e.printStackTrace();
            return Collections.emptyMap();
        }

    }
    public static Map toMap(File file) throws Exception{

        try {
            return objectMapper.readValue(file,Map.class);
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }
    public static JsonNode toJsonNode(String jsonStr) throws Exception{
        try {
            JsonNode root=objectMapper.readTree(jsonStr);
            return root;
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }
    public static JsonNode toJsonNode(File file) throws Exception{
        try {
            JsonNode root=objectMapper.readTree(file);
            return root;
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }
    public static JsonNode getChildNode(JsonNode node,String nodeName){
        return node.path(nodeName);
    }
}
